import Vue from 'vue'
import { formatDate } from '@/common/utils/date'

Vue.filter('formatThousand', e => {
  if (typeof e === 'undefined' || e === null) {
    return '--'
  } else {
    return e.toFixed(2).replace(/\B(?=(\d{3})+(?!\d))/g, ',')
  }
})

Vue.filter('formatTenThousand', e => {
  if (typeof e === 'undefined' || e === null) {
    return '--'
  } else if (e === 0) {
    return 0
  }
  return (e / 1000).toFixed(2)
})

Vue.filter('tranformUndefined', e => {
  if (typeof e === 'undefined' || e === null) {
    return '--'
  } else {
    return e
  }
})

Vue.filter('date', (timeStamp, format = 'yyyy-MM-dd HH:mm') => {
  if (!timeStamp) return ''
  return formatDate(format, timeStamp)
})

Vue.filter('price', (price, fixed = 0) => {
  if (!price || isNaN(price)) return 0
  return fixed > 0 ? +parseFloat(price).toFixed(fixed) : +parseFloat(price).toFixed(2)
})
